import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wanchong/api/store.dart';

import 'package:wanchong/app/store/details.dart';
import 'package:wanchong/app/store/post.dart';
import 'package:wanchong/bean/invitation_entity.dart';

//import 'package:wanchong/bean/invitation.dart';

import 'package:wanchong/conf.dart';
import 'package:wanchong/path.dart';
import 'package:wanchong/widget/marquee_continuous.dart';
import 'package:wanchong/widget/store_app_bar.dart';
import 'package:wanchong/widget/store_header.dart';
import 'package:wanchong/widget/store_item.dart';

// import 'dart:math';
class StroeHome extends StatefulWidget {
  // var _w = MediaQuery.of(context).padding.top;
  @override
  _StroeHomeState createState() => _StroeHomeState();
}

class _StroeHomeState extends State<StroeHome> {
  ScrollController _controller = ScrollController();

  ///同城帖子
  List<InvitationData> vitations = [];
  var _opt = 0.0;
  @override
  void initState() {
    // getInvitation((res) {
    //   setState(() {
    //     vitations = res;
    //   });
    // });
    getInvitation().listen((InvitationEntity data) {
      setState(() {
        vitations = data.data;
      });
    });
    super.initState();
  }

  // var _w = MediaQuery.of(context).padding.top;
  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      controller: _controller,
      slivers: <Widget>[
        // SliverPersistentHeader(delegate: ,),
        StoreSliverAppBar(
          // leading: Container(color: Colors.red,),
          pinned: true,
          // backgroundColor: Colors.red,
          // textTheme: TextTheme(),
          // title: Text("hello world"),
          centerTitle: true,
          // floating: true,
          expandedHeight: ScreenUtil().setHeight(240.0),
          backgroundColor: Colors.transparent,
          flexibleSpace: FlexibleSpaceBar(
            centerTitle: true,
            title: _search(),
            background: _background(),
          ),
        ),
        SliverPersistentHeader(
          // pinned: true,
          delegate: StoreHeaderDelegate(
              minHeight: ScreenUtil().setHeight(35.0),
              maxHeight: ScreenUtil().setHeight(35.0),
              child: vitations.length > 0
                  ? MarqueeContinuous(
                      children: vitations.map((e) {
                        return Row(
                          children: <Widget>[
                            Container(
                              // color: Colors.yellow,
                              width: ScreenUtil().setHeight(15),
                              margin:
                                  EdgeInsets.all(ScreenUtil().setHeight(10)),
                              child: Image.asset(
                                  "assets/store/shop_icon_chengshi.png"),
                            ),
                            Text(
                              e.city,
                              style: TextStyle(
                                color: Color(MainColor),
                                fontSize: ScreenUtil().setSp(14.0),
                              ),
                            ),
                            SizedBox(
                              width: ScreenUtil().setWidth(6.0),
                            ),
                            GestureDetector(
                              onTap: (){
                                Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context)=>StorePost(id: 4,)));
                              },
                              // child: Expanded(
                              //   child: Text(
                              //     e.title,
                              //     style: TextStyle(
                              //       color: Color(MainColor),
                              //       fontSize: ScreenUtil().setSp(14.0),
                              //     ),
                              //   ),
                              child: Text(
                                  e.title,
                                  style: TextStyle(
                                    color: Color(MainColor),
                                    fontSize: ScreenUtil().setSp(14.0),
                                  ),
                              ),
                            ),
                            SizedBox(
                              width: ScreenUtil().setWidth(15.0),
                            ),
                            Container(
                              height: ScreenUtil().setHeight(15.0),
                              width: ScreenUtil().setWidth(1.0),
                              color: Color(BColor),
                            ),
                            GestureDetector(
                              child: Container(
                                width: ScreenUtil().setWidth(64.0),
                                child: Image.asset(
                                    "assets/store/shop_icon_new.png"),
                              ),
                            )
                          ],
                        );
                      }).toList(),
                      width: 345.0,
                      height: ScreenUtil().setHeight(35.0),
                      count: vitations.length,
                      duration: Duration(seconds: 3),
                    )
                  : Container()),
        ),
        // SizedBox(height: 15.0,),
        SliverToBoxAdapter(
          child: Container(
            margin: EdgeInsets.only(top: ScreenUtil().setHeight(15.0)),
            color: Colors.white,
            width: double.infinity,
            padding: EdgeInsets.only(
              left: ScreenUtil().setWidth(15.0),
              top: ScreenUtil().setHeight(15.0),
              bottom: ScreenUtil().setHeight(15.0),
            ),
            child: Text(
              "合作门店",
              style: TextStyle(
                  color: Color(MainColor),
                  fontSize: ScreenUtil().setSp(14.0),
                  fontWeight: FontWeight.w700),
            ),
          ),
        ),

        SliverPadding(
          padding: EdgeInsets.only(
            left: 0.0,
            right: 0.0,
          ),
          sliver: SliverGrid(
            //Grid
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 1, //Grid按两列显示
              mainAxisSpacing: 0.0,
              // crossAxisSpacing: 10.0,
              childAspectRatio: 4,
            ),
            delegate: new SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                //创建子widget
                return StoreItem(
                  storeImg: CachedNetworkImage(
                    imageUrl:
                        "http://b-ssl.duitang.com/uploads/item/201503/04/20150304121513_c5LEa.jpeg",
                    fit: BoxFit.cover,
                  ),
                  storeName: "其齐鲁宠物店",
                  addr: "北京",
                  addrName: "九方购物中心",
                  catName: "宠物医院",
                  tags: List.generate(6, (index) => "标签$index"),
                  distance: index * 30.0,
                  onPress: () {
                    // alert("aaaa");
                    Navigator.of(context).push(MaterialPageRoute(
                        builder: (BuildContext context) => StoreDetails(
                              id: "aaaa",
                            )));
                    //Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context)=>StoreDetails(id: "aaaa",)));

                  },
                );
              },
              childCount: 50,
            ),
          ),
        ),
      ],
    );
  }

  _background() {
    return Container(
      padding: EdgeInsets.only(left: 16.0, right: 16.0),
      decoration: BoxDecoration(
        image: DecorationImage(
            image: AssetImage("assets/store/my_bg_top_2_img.png"),
            fit: BoxFit.cover),
      ),
      child: SafeArea(
        child: Stack(
          children: <Widget>[
            Positioned(
              // left: 16.0,
              top: 15.0,
              child: Row(
                children: <Widget>[
                  Container(
                    width: ScreenUtil().setWidth(20.0),
                    height: ScreenUtil().setHeight(20.0),
                    child: Image.asset("assets/store/shop_icon_dingwei.png"),
                  ),
                  Text(
                    "天府新谷（华府大道..)",
                    style: TextStyle(color: Color(0xFF000000), fontSize: 18.0),
                  ),
                ],
              ),
            ),
            Positioned(
              right: 0.0,
              top: 15.0,
              child: Container(
                // margin: EdgeInsets.only(right: 5.0),
                width: ScreenUtil().setWidth(20.0),
                height: ScreenUtil().setHeight(20.0),
                child: Image.asset("assets/store/my_icon_xiaoxi.png"),
              ),
            ),
            Positioned(
              height: 35.0,
              top: 53,
              child: Container(
                width: MediaQuery.of(context).size.width - 34,
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(
                    color: Colors.white,
                  ),
                  borderRadius: BorderRadius.circular(6.0),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.only(right: 5.0),
                      width: ScreenUtil().setWidth(15.0),
                      height: ScreenUtil().setHeight(15.0),
                      child:
                          Image.asset("assets/store/shop_list_sousuo_icon.png"),
                    ),
                    Text(
                      "输入商家，服务名称",
                      style: TextStyle(color: Color(BColor), fontSize: 14.0),
                    )
                  ],
                ),
              ),
            ),
            Positioned(
              top: 125,
              child: Container(
                  alignment: Alignment.center,
                  // color: Colors.yellow,
                  width: MediaQuery.of(context).size.width - 34,
                  height: 90.0,
                  child: Image.asset("assets/store/shop_ad_img.png")),
            )
          ],
        ),
      ),
    );
  }

  _search() {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pushNamed(SearchPath);
      },
      child: Container(
        height: 35.0,
        width: MediaQuery.of(context).size.width - 34,
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border.all(
            color: Colors.white,
          ),
          borderRadius: BorderRadius.circular(6.0),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(right: 5.0),
              width: ScreenUtil().setWidth(15.0),
              height: ScreenUtil().setHeight(15.0),
              child: Image.asset("assets/store/shop_list_sousuo_icon.png"),
            ),
            Text(
              "输入商家，服务名称",
              style: TextStyle(color: Color(0xFFB4BEC6), fontSize: 14.0),
            )
          ],
        ),
      ),
    );
  }
}
